<?php

/**
 * Flickr Photosets - Index Page
 * Displays information from your friends.
 * 
 * @author Brad Dougherty <bdougherty@gmail.com>
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License 3.0
 */

/*
Require the function library
This will include all other necessary files
*/
require_once 'lib.php';

/*
Initialize Flickr Photosets
*/
$fp = new FP_Canvas(FP_Canvas::uid_from_viewer, false, false, false);

if ($fp->uid) {
	
	// Get info and initialize
	$fp->get_info();
	
	if ($fp->is_flickr_authorized()) {
		$fp->init_flickr();
		$fp->flickr->setToken($fp->config['flickr_token']);

		// Prepare contents for your recent photos
		if ($fp->config['privacy_filter'] == $fp->privacy_filter['private']) {
			$photos = $fp->flickr->photos_search(array(
				'user_id'        => $fp->nsid,
				'per_page'       => 6,
				'extras'         => 'date_upload, media',
			));
		}
		else {
			$photos = $fp->flickr->photos_search(array(
				'user_id'        => $fp->nsid,
				'privacy_filter' => $fp->privacy_filter['public'],
				'per_page'       => 6,
				'extras'         => 'date_upload, media',
			));
		}
	}
		
}

// Load stylesheet
echo $fp->get_stylesheet('index');

?>

<fb:if-is-app-user>
	
	<?=$fp->get_header('home', '<fb:application-name linked="false" />', true)?>
	
	<?php if (!$fp->is_flickr_authorized()): ?>
		<div class="boxes clearfix">
			<div class="left_column">

				<div class="box" id="account_box">
					<h3>Flickr Account</h3>
					<div class="inside">

						<p>Looks like you haven't set up your Flickr account yet. Click on the Authenticate button below to setup your Flickr account. When you click Authenticate, you will be redirected to Flickr to authorize access.</p>
						
						<form action="<?=$fp->callback_url?>/auth.php">
							<div class="buttons">
								<input type="hidden" name="write" />
								<input type="submit" class="button" value="Authenticate" />
							</div>
						</form>

					</div>
				</div>

			</div>
		</div>
	<?php else: ?>
		
		<?php if (isset($_GET['added'])) {
			echo '<fb:success><fb:message>Your Flickr account was added!</fb:message>You can go to the <a href="'.$fp->canvas_url.'/configure/">configuration page</a> to change your settings.</fb:success>';
		} ?>
		
		<?php if (isset($_GET['profileupdated'])): ?>
			<div style="overflow: hidden; margin-bottom: -21px;" id="profile_updated"><p class="message">Your profile was successfully updated.</p></div>
			<script type="text/javascript">
				Animation(document.getElementById('profile_updated')).duration(3000).checkpoint().to('height', '0px').to('opacity', 0).to('margin-bottom', 0).hide().ease(Animation.ease.end).go();
			</script>
		<?php endif; ?>
		
		<div class="boxes clearfix">
			<div class="left_column" style="margin-top: -16px;">
				
				<div id="recent_photos">
					<div class="grid_header"><h3>Your recent photos</h3></div>
					<div class="photo_grid">
						<table>
							<tr><? $i = 0; foreach ($photos['photo'] as $photo): ?>
								<td><a href="<?=$fp->canvas_url?>/photos/<?=$fp->uid?>/<?=$photo['id']?>/">
									<fb:user-agent includes="ie 6">
										<img src="<?=$fp->flickr->buildPhotoUrl($photo, 'thumbnail')?>" alt="<?=$photo['title']?>" title="<?=$photo['title']?>" />
									</fb:user-agent>
									<fb:user-agent excludes="ie 6">
										<img src="<?=$fp->flickr->buildPhotoUrl($photo, 'small')?>" alt="<?=$photo['title']?>" title="<?=$photo['title']?>" />
									</fb:user-agent>
								</a></td>
							<? $i++;
							if ($i % 3 == 0) {
								echo '</tr><tr>';
							}
							endforeach; ?></tr>
						</table>
					</div>
				</div>
				
			</div>
			<div class="right_column">
				
				<p class="message">What would you like to see on this page? <a href="http://flickrphotosets.tumblr.com/post/49967504/redesign-launch">Send us your feedback!</a></p>
				
				<div class="box" id="profile_box">
					<h3>Profile Box</h3>
					<div class="inside">
						<p>Your profile box was last updated <?=date('F j, Y \a\\t g:i A (T)', $fp->get_last_profile_update())?></p>
						<p><a href="<?=$fp->canvas_url?>/update/?fromindex">Update Now</a></p>
					</div>
				</div>
				
			</div>
		</div>
		
		<div class="clearfix" style="margin-top: 29px;"></div>
		
	<?php endif; ?>
	
<fb:else>
	
	<?=$fp->get_header('home', '<fb:application-name linked="false" />', true)?>
	
	<div class="big_indent bottom_margin">
		<h1>Welcome to <fb:application-name linked="false" />!</h1>
		
		<p>To get started, you'll need to <a href="<?=$fp->facebook->get_login_url('', true)?>">authorize your Flickr account</a>.</p>
		
		<p><fb:application-name linked="false" /> allows you to display your <a href="http://www.flickr.com">Flickr</a> photos inside Facebook. You do not need to authorize the app in order to view photos from your friends with Flickr accounts.</p>
		
	</div>
	
</fb:else></fb:if-is-app-user>

<?=$fp->echo_footer(true)?>